// 小程序各运行环境请求接口地址
// #ifdef MP-WEIXIN
let SERVER = __wxConfig.envVersion;
if(SERVER == "develop"){//开发版
	// var BASE_URL = 'http://dev.ditanshequ.vip/api'
	var BASE_URL = 'http://test-api.ditanshequ.vip/api'
	// var BASE_URL = 'http://local.phsq.com/api'
	// var BASE_URL = 'https://api.ditanshequ.vip/api'
}else if(SERVER == "trial"){//体验版
	var BASE_URL = 'http://test-api.ditanshequ.vip/api'
}else if(SERVER == "release"){//线上版
	var BASE_URL = 'https://api.ditanshequ.vip/api'
}
// #endif

// appH5各运行环境请求接地址
// #ifndef MP-WEIXIN
let SERVERS = process.env.NODE_ENV;//判断是按照南方请、
// let SERVERS = 4
if(SERVERS === "development"){//开发环境
	// var BASE_URL = 'http://local.phsq.com/api'
	// var BASE_URL = 'http://dev.ditanshequ.vip/api'
	// var BASE_URL = 'http://test-api.ditanshequ.vip/api'
	// var BASE_URL = 'https://api.ditanshequ.vip/api'
	var BASE_URL = 'http://test-api.ditanshequ.vip/api'
	var BASE_SHOP = 'http://hzh.chinahuishe.com/index.php'
}else {//生产环境
	// var BASE_URL = 'http://dev.ditanshequ.vip/api'
	// var BASE_URL = 'http://test-api.ditanshequ.vip/api'
	// var BASE_URL = 'https://api.ditanshequ.vip/api'
	// var BASE_URL = 'http://test-api.ditanshequ.vip/api'
	var BASE_SHOP = 'http://hzh.chinahuishe.com/index.php'
	var BASE_URL = 'https://api.ditanshequ.vip/api'
}
// #endif
// app分享基础域名
function shareBaseUrl(){
	// return 'http://test-mobile.ditanshequ.vip'
	return 'http://mobile.ditanshequ.vip/'
}

function baseUrl() {
	return BASE_URL
}

function url() {
	return URL
}
// 请求接口封装
function reqjson(url, data, method,callback) {
	let token = uni.getStorageSync('Authori-zation')
	return new Promise((resolve, reject) => {
		let header = {
			'Authori-zation': 'Bearer' + ' ' + token,
			"Content-Type": "application/json"
		}
		uni.request({
			url: BASE_URL + url,
			data,
			method,
			header,
			success: (res) => {
				uni.hideLoading()
				let status = res.data.status
				if (status == 200) {
					resolve(res.data)
				} else if ([410000, 410001, 410002,410003].indexOf(status) !== -1) {
					uni.navigateTo({
						url: '/pages/login/login'
					});
				} else {
					resolve(res.data)
					$model(res.data.msg)
					if (callback) {
						callback
					}
				}
			},
			fail: (err) => {
				if (err.errMsg == 'request:fail timeout') {
					$model('请求超时，请检查网络')
				}
				reject(err)
			}
		})
	})
}
//微信请求封装
function request(obj) {
	let token = uni.getStorageSync('Authori-zation');
	let url = 'http://hzh.chinahuishe.com/index.php?s='+ obj.url
	return new Promise((resolve, reject) => {
		let header = {
			// 'Authori-zation': 'Bearer' + ' ' + token,
			"Content-Type": "application/json"
		}
		obj.data = obj.data||{};
		obj.data.token =  uni.getStorageSync('Authori-zation');
		obj.data.wxapp_id = 10001; 
		console.log('url',url)
		uni.request({
			url: url ,
			data:obj.data,
			method:obj.method||'GET',
			header,
			success: (res) => {
				uni.hideLoading();
				let status = res.data.code
				if (status == 1) {
					resolve(res.data)
				} else if ([410000, 410001, 410002,410003].indexOf(status) !== -1) {
					uni.navigateTo({
						url: '/pages/login/login'
					});
				} else {
					$model(res.data.msg)
					obj.error&&obj.error();
				}
			},
			fail: (err) => {
				if (err.errMsg == 'request:fail timeout') {
					$model('请求超时，请检查网络')
				}
				reject(err)
			}
		})
	})
}


// 新增注册封装
function newRegister(url, data, method,callback) {
	let token = uni.getStorageSync('Authori-zation')
	return new Promise((resolve, reject) => {
		let header = {
			'Authori-zation': 'Bearer' + ' ' + token,
			"Content-Type": "application/json"
		}
		uni.request({
			url: BASE_URL + url,
			data,
			method,
			header,
			success: (res) => {
				let status = res.data.status
				if (status == 200) {
					resolve(res.data)
				} else if ([410000, 410001, 410002,410003].indexOf(status) !== -1) {
					uni.navigateTo({
						url: '/pages/login/login'
					});
				} else {
					uni.hideLoading()
					resolve(res.data)
					$model(res.data.msg)
					if (callback) {
						callback
					}
				}
			},
			fail: (err) => {
				if (err.errMsg == 'request:fail timeout') {
					$model('请求超时，请检查网络')
				}
				reject(err)
			}
		})
	})
}

// 商城GET接口
function shopping(url, data, method,callback) {
	return new Promise((resolve, reject) => {
		let header = {
			'token': uni.getStorageSync('Authori-shop'),
			"Content-Type": 'application/json'
		}
		uni.request({
			url: BASE_SHOP,
			data,
			method,
			header,
			success: (res) => {
				uni.hideLoading()
				let status = res.data.code
				if (status == 1) {
					resolve(res.data)
				} else if ([410000, 410001, 410002,410003].indexOf(status) !== -1) {
					uni.navigateTo({
						url: '/pages/login/login'
					});
				} else {
					resolve(res.data)
					$model(res.data.msg)
					if (callback) {
						callback
					}
				}
			},
			fail: (err) => {
				console.log(err)
				if (err.errMsg == 'request:fail timeout') {
					$model('请求超时，请检查网络')
				}
				reject(err)
			}
		})
	})
}

// 商城POST接口
export function shopPost(url, data, method,callback) {
	return new Promise((resolve, reject) => {
		let hostUrl = BASE_SHOP
		let jsonArr = ''
		for (let i in data) {
			jsonArr += i + '=' + data[i] + '&'
		}
		jsonArr += 'token=' + uni.getStorageSync('Authori-shop')
		hostUrl = hostUrl + '?' + jsonArr
		uni.request({
			url: BASE_SHOP + '?s=' + url,
			data,
			method,
			header: {
				'token': uni.getStorageSync('Authori-shop'),
				"Content-Type": 'application/json'
			},
			success: (res) => {
				uni.hideLoading()
				let status = res.data.code
				if (status == 1) {
					resolve(res.data)
				} else if ([410000, 410001, 410002,410003].indexOf(status) !== -1) {
					uni.navigateTo({
						url: '/pages/login/login'
					});
				} else {
					resolve(res.data)
					$model(res.data.msg)
					if (callback) {
						callback
					}
				}
			},
			fail: (err) => {
				console.log(err)
				if (err.errMsg == 'request:fail timeout') {
					$model('请求超时，请检查网络')
				}
				reject(err)
			}
		})
	})
}

function $model(msg, showCancel = false) {
	uni.showModal({
		title: '提示',
		content: msg,
		showCancel,
	})
}

const msg = (title, duration=1500, mask=false, icon='none')=>{
	if(Boolean(title) === false){
		return;
	}
	uni.showToast({
		title,
		duration,
		mask,
		icon
	});
}

const deciml = function(num){
	    var resu;
	    var reg = /([0-9]+.[0-9]{2})[0-9]*/;
	    resu = num.replace(reg,"$1");
		return resu
}
export {
	reqjson,
	baseUrl,
	url,
	$model,
	msg,
	deciml,
	shareBaseUrl,
	newRegister,
	shopping
}
